
#include <luajit-2.0/lua.h>
#include <luajit-2.0/lualib.h>
#include <luajit-2.0/lauxlib.h>
#include <SDL2/SDL.h>
//#include <SDL/SDL.h>

AUDIO_S16SYS

#define lua_isstring(a,idx) (lua_type(a,idx)==LUA_TSTRING)
#ifndef lcheck_toptr
#define lcheck_toptr _lcheck_toptr
static void* _lcheck_toptr(lua_State*L,int idx){
  void* data = 0;
  if(lua_type(L,idx)==0xA){
      data = *(void**)lua_topointer(L,idx);
  }
  else if(lua_isstring(L,idx)){
    data = (void*)lua_tostring(L,idx);
  }
  else if(lua_islightuserdata(L,idx)/*||lua_isuserdata(L,idx)*/){
    data = (void*)lua_touserdata(L,idx);
  }
  else if(lua_isuserdata(L,idx)){
    data = *(void**)lua_touserdata(L,idx);
  }
  else{
    luaL_argerror(L,idx,"type error:pointer_type");
  };
  return data;
}
#endif

static int laaa(lua_State*L){
    void*p = _lcheck_toptr(L,1);
    printf("%p\n",p);
    return 0;

}

int main(int argc,char**argv){
    lua_State*L = luaL_newstate();
    lua_register(L,"aaa",laaa);
    luaL_openlibs(L);
    luaL_dostring(L,"ffi = require('ffi');aaa(ffi.cast('void*',0x22))");
    lua_close(L);
}

